<?php
namespace app\admin\controller;
/**
 * #财务管理}
 */
class Finance extends Common{
    /**
     * 财务统计
     * @return [type] [description]
     */
    function index($range=''){
        if(is_string($range)){
            $range=explode(',', $range);
        }
        $page=input('page/d',1);
        $s=($page-1)*config('paginate.list_rows');
        
        $params=[
            'range'=>$range,
        ];
        $start=0;
        $end=time();
        $map=[];
        if(!empty($range)){
            if(count($range)===2){
                $start=$range[0]/1000;
                $end=$range[1]/1000+86400;
                $map['now_time']=['now_time','between',[$start,$end]];
            }else{
                $start=$range[0]/1000;
                $map['now_time']=['now_time','>=',$start];
            }
        }
        $res=model('finance')->statis([
            'start'=>$start,
            'end'=>$end,
            's'=>$s,
            'isChart'=>false,
            'order'=>'order by id desc',
        ]);
        $total=model($this->table)->where($map)->group('now_time')->count();
        $this->assign('total',$total);
        $list=$res[0];
        $all=$res[1];
        foreach ($list as $k => $v) {
            $list[$k]['income']=$v['vip_buy']+$v['coin_buy'];
            $list[$k]['expenditure']=$v['withdraw_cash'];
            $list[$k]['profit']=number_format(($v['vip_buy']+$v['coin_buy'])-$v['withdraw_cash'],2);
        }

        $all['income']=$all['vip_buy']+$all['coin_buy'];
        $all['expenditure']=$all['withdraw_cash'];
        $all['profit']=($all['vip_buy']+$all['coin_buy'])-$all['withdraw_cash'];
        $this->assign('params',json_encode($params));
        $this->assign('list',json_encode($list));
        $this->assign('all',json_encode($all));
        return view();
    }

    /**
     * 奖金领取列表
     * @return [type] [description]
     */
    function bonus($type=-1,$now_time=null,$range=''){
        if(is_string($range)){
            $range=explode(',', $range);
        }

        $params=[
            'range'=>$range,
            'type'=>$type,
            'cate'=>input('cate/d',1),
            'keyword'=>input('keyword/s',''),
        ];
        $map=[];
        if($type>=0){
            $map['type']=$type;
        }
        if(!empty($params['keyword'])){
            if($params['cate']==1){
                $user=model('member')->readAll([
                    'name|tel'=>['name|tel','like',"%{$params['keyword']}%"]
                ],'id');
                $ids=[];
                foreach ($user as $v) {
                    $ids[]=$v['id'];
                }
                $map['uid']=['uid','in',$ids];
            }elseif($params['cate']==2){
                $from=model('member')->readAll([
                    'name|tel'=>['name|tel','like',"%{$params['keyword']}%"]
                ],'id');
                $ids=[];
                foreach ($from as $v) {
                    $ids[]=$v['id'];
                }
                $map['from_id']=['from_id','in',$ids];
            }elseif($params['cate']==3){
                $order=model('order')->readAll([
                    'number'=>['number','like',"%{$params['keyword']}%"]
                ],'id');
                $ids=[];
                foreach ($order as $v) {
                    $ids[]=$v['id'];
                }
                $map['order_id']=['order_id','in',$ids];
            }
        }
        if(!is_null($now_time)){
            $params['now_time']=$now_time;
            $params['range']=[$now_time*1000,$now_time*1000];
            $range=$params['range'];
        }
        if(!empty($range)){
            if(count($range)===2){
                $map['s_time']=['s_time','between',[$range[0]/1000,$range[1]/1000+86400]];
            }else{
                $map['s_time']=['s_time','>=',$range[0]/1000];
            }
        }
        $options=[
            'order'=>'id desc',
            'with'=>['user','from','order']
        ];
        $list=model('reward')->readAll($map,true,$options)->appends($params);
        $this->assign('list',$list);
        $this->assign('params',json_encode($params));
        return view();
    }
    /**
     * 所有的消费记录
     * @param  integer $type     [description]
     * @param  string  $memo     [description]
     * @param  integer $now_time [description]
     * @param  string  $range    [description]
     * @return [type]            [description]
     */
    function finance($type=-1,$memo=null,$now_time=null,$range=''){
        if(is_string($range)){
            $range=explode(',', $range);
        }
        $params=[
            'range'=>$range,
            'type'=>$type,
            'cate'=>input('cate/d',1),
            'keyword'=>input('keyword/s',''),
        ];
        $map=[];
        if($type>=0){
            $map['type']=$type;
        }
        if(!is_null($memo)){
            $map['memo']=$memo;
        }
        if(!empty($params['keyword'])){
            if($params['cate']==1){
                $user=model('member')->readAll([
                    'name|tel'=>['name|tel','like',"%{$params['keyword']}%"]
                ],'id');
                $ids=[];
                foreach ($user as $v) {
                    $ids[]=$v['id'];
                }
                $map['uid']=['uid','in',$ids];
            }elseif($params['cate']==2){
                $from=model('member')->readAll([
                    'name|tel'=>['name|tel','like',"%{$params['keyword']}%"]
                ],'id');
                $ids=[];
                foreach ($from as $v) {
                    $ids[]=$v['id'];
                }
                $map['from_id']=['from_id','in',$ids];
            }elseif($params['cate']==3){
                $order=model('order')->readAll([
                    'number'=>['number','like',"%{$params['keyword']}%"]
                ],'id');
                $ids=[];
                foreach ($order as $v) {
                    $ids[]=$v['id'];
                }
                $map['order_id']=['order_id','in',$ids];
            }
        }
        if(!empty($range)){
            if(count($range)===2){
                $map['s_time']=['s_time','between',[$range[0]/1000,$range[1]/1000+86400]];
            }else{
                $map['s_time']=['s_time','>=',$range[0]/1000];
            }
        }
        if(!is_null($now_time)){
            $map['now_time']=$now_time;
            $params['now_time']=$now_time;
            $params['range']=[$now_time*1000,$now_time*1000];
        }
        $options=[
            'order'=>'id desc',
            'with'=>['user','from','order']
        ];
        $list=model($this->table)->readAll($map,true,$options)->appends($params);
        $this->assign('list',$list);
        $this->assign('params',json_encode($params));
        return view();
    }
}